<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="activity_id" label="活动" required>
				<uni-data-picker v-model="formData.activity_id" collection="qmy-activity"
					field="name as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="signup_amount" label="报名费用">
				<uni-easyinput placeholder="报名费用" type="number" v-model="formData.signup_amount"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="order_id" label="付款订单">
				<uni-easyinput placeholder="关联的付款订单号，关联订单表" v-model="formData.order_id"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="signup_status" label="报名状态" required>
				<uni-data-checkbox placeholder="报名状态,0:已报名,1:已取消" type="number" :localdata="range"
					v-model="formData.signup_status"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="user_id" label="报名用户" required>
				<uni-data-picker v-model="formData.user_id" collection="uni-id-users"
					field="nickname as text, _id as value"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="signup_time" label="报名时间" required>
				<uni-datetime-picker return-type="timestamp" v-model="formData.signup_time"></uni-datetime-picker>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/qmy-activity-sign-up.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'qmy-activity-sign-up';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"activity_id": "",
				"signup_amount": null,
				"order_id": "",
				"signup_status": 0,
				"user_id": "",
				"signup_time": null
			}
			return {
				formData,
				formOptions: {},
				range: [{
					"value": 0,
					"text": "已报名"
				}, {
					"value": 1,
					"text": "已取消"
				}],
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					"activity_id,signup_amount,order_id,signup_status,user_id,signup_time").get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>